﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace umodelGUILE
{
    class IniUtil
    {
        private string iniPath;
        public const int MAX_SIZE = 255;

        public IniUtil(string path)
        {
            this.iniPath = path;
        }

        [DllImport("kernel32.dll")]
        private static extern int GetPrivateProfileString(
            string section,             // 섹션값 (ini파일에서의 대분류)
            string key,                 // 키값
            string def,                 // 디폴트값
            StringBuilder loadValue,   // 불러올 데이터
            int size,                   // 데이터 사이즈
            string iniPath);            // ini파일 경로

        [DllImport("kernel32.dll")]
        private static extern long WritePrivateProfileString(
            string section,             // 섹션값 (ini파일에서의 대분류)
            string key,                 // 키값
            string saveVlaue,            // 저장할 데이터
            string iniPath);            // ini파일 경로

        /// <summary>
        /// ini Stirng값 읽어오기
        /// </summary>
        /// <param name="section">섹션값</param>
        /// <param name="key">키값</param>
        /// <returns>저장되어 있는 String값 리턴</returns>
        public string GetIniValueString(string section, string key)
        {
            StringBuilder temp = new StringBuilder(MAX_SIZE);
            int i = GetPrivateProfileString(section, key, "", temp, MAX_SIZE, iniPath);
            return temp.ToString();
        }

        /// <summary>
        /// ini Boolean값 읽어오기
        /// </summary>
        /// <param name="section">세션값</param>
        /// <param name="key">키값</param>
        /// <param name="def">기본값</param>
        /// <returns>저장되어 있는 Boolean값 리턴</returns>
        public bool GetIniValueBoolean(string section, string key, bool def)
        {
            StringBuilder temp = new StringBuilder(MAX_SIZE);
            int i = GetPrivateProfileString(section, key, "", temp, MAX_SIZE, iniPath);
            if (temp.ToString().Equals(""))
            {
                return def;
            } // 저장되어 있는 값이 없으면 기본값 리턴
            if (temp.ToString().Equals("1"))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// ini String값 저장
        /// </summary>
        /// <param name="section">섹션값</param>
        /// <param name="key">키값</param>
        /// <param name="value">저장할 String값</param>
        public void SetIniValueString(string section, string key, string value)
        {
            WritePrivateProfileString(section, key, value, iniPath);
        }

        /// <summary>
        /// ini Boolean값 저장
        /// </summary>
        /// <param name="section">섹션값</param>
        /// <param name="key">키값</param>
        /// <param name="value">저장할 Boolean값</param>
        public void SetIniValueBoolean(string section, string key, bool value)
        {
            WritePrivateProfileString(section, key, value ? "1" : "0", iniPath);
        }
    }
}
